//
//  NewsViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-5-2.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "NewsViewController.h"
#import "AppsEngine.h"
#import "GFVOParser.h"
#import "CacheDaoForfficalNews.h"
#import "JSON.h"

#import "AdViewData.h"
#import "AdView.h"
#import "UserSession.h"

@interface NewsViewController () {
    NSMutableArray *officalNewsArray;
    AdView *aView;
    NSMutableArray *aDataSource;
}

@end

@implementation NewsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private
- (void)initData {
    //读取缓存数据，再请求网上数据
    NSMutableArray *cDataArray = [[CacheDaoForfficalNews shareCacheDaoForfficalNews] getCacheDataLimitRow:NSMakeRange(0, 5) newsType:0];
    if (cDataArray && [cDataArray count] > 0) {
        if (officalNewsArray) {
            [officalNewsArray removeAllObjects];
        }
        officalNewsArray = cDataArray;
        [self refreshImageScrollView];
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[[UserSession shareUserSession] getUserId],@"userId", nil];
    //网上请求数据
    [[AppsEngine shareAppsEngine] submitRequest:@"gfGetNewsList" param:params onCompletion:^(id jsonResponse) {
        
        if ([jsonResponse objectForKey:@"status"] && [[jsonResponse objectForKey:@"status"] isEqualToString:@"success"]) {
            NSLog(@"刷新成功");
            NSMutableArray *arr1 = [[GFVOParser shareParser] paseDataForOfficalNewsContentArray:jsonResponse];
            if (!arr1 || [arr1 count] <= 0) {
                NSLog(@"没有数据了...");
            }
            else {
                [officalNewsArray removeAllObjects];
                officalNewsArray = arr1;
                [[CacheDaoForfficalNews shareCacheDaoForfficalNews] updateCacheDataWithNewsContentArray:officalNewsArray];
                [self refreshImageScrollView];
            }
        }
    } onError:^(NSError *error) {
        NSLog(@"联网失败");
    } method:@"GET"];
}

- (void)refreshImageScrollView {
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    if ([officalNewsArray count] > 5) {
        arr = [[NSMutableArray alloc] initWithArray:[officalNewsArray subarrayWithRange:NSMakeRange(0, 5)]];
    }
    else {
        arr = officalNewsArray;
    }
    
    aDataSource = [[NSMutableArray alloc] init];
    for (int i = 0; i < [arr count]; i++) {
        NSDictionary *content = [arr objectAtIndex:i];
        if (isValidDictionary([content objectForKey:@"sourceLinks"])) {
            NSArray *imgs = [[content objectForKey:@"sourceLinks"] objectForKey:@"image"];
            
            AdViewData *aData = [[AdViewData alloc] init];
            aData.adWords = [content objectForKey:@"title"];
            aData.webImageLink = [imgs count] > 0 ? [imgs objectAtIndex:0] : nil;
            aData.tag = i;
            aData.dataInfo = content;
            aData.recordId = [[content objectForKey:@"newsId"] integerValue] ? [[content objectForKey:@"newsId"] integerValue] : [[content objectForKey:@"id"] integerValue];
            [aDataSource addObject:aData];
        }
    }
    
    if (!aView) {
        aView = [[AdView alloc] init];
        [aView setFrame:CGRectMake(0.f, 0.f, 320.f, self.view.frame.size.height)];
        [self.view addSubview:aView];
    }
    [aView setSourceArr:aDataSource];
    [aView loadAds];
}




@end
